﻿using Coding4Fun.MissionControl.WP.Models;
using Coding4Fun.MissionControl.WP.ViewModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;

namespace Coding4Fun.MissionControl.WP.Misc
{
    public class ResponseHelper
    {
        public static void HandleIncomingResponse(string rawResponse)
        {
            if (rawResponse != null)
            {
                ServerResponse response = JsonConvert.DeserializeObject<ServerResponse>(rawResponse);

                if (response.Marker == Constants.COMMAND_SERVER_NOTIFICATION)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(response.Content, "Server Response", MessageBoxButton.OK);
                    });
                }
                else
                {
                    if (CommonViewModel.Instance.IsWaiting)
                    {
                        if (response.Identifier == CommonViewModel.Instance.CurrentServer.Location)
                        {
                            // returns the list of sets that are associated with the current server.
                            if (response.Marker == Constants.COMMAND_SERVER_SET_LIST)
                            {

                                List<string> items = JsonConvert.DeserializeObject<List<string>>(response.Content);

                                if (items != null)
                                {
                                    List<Group<string>> groupedItems = Group<string>.CreateGroups(items,
                                        CultureInfo.CurrentCulture, (string s) => { return s[0].ToString(); }, true);
                                    SetsPageViewModel.Instance.Sets = groupedItems;
                                }
                                else
                                {
                                    SetsPageViewModel.Instance.Sets = new List<Group<string>>();
                                }

                                Deployment.Current.Dispatcher.BeginInvoke(() =>
                                    {
                                        CommonViewModel.Instance.IsWaiting = false;

                                        if (!App.RootFrame.CurrentSource.ToString().Contains("SetsPage"))
                                        {
                                            App.RootFrame.Navigate(new Uri("/Views/SetsPage.xaml", UriKind.Relative));
                                        }
                                    });

                            }
                            // returns the list of commands associated with a given set.
                            else if (response.Marker.Contains(Constants.COMMAND_SERVER_SET_COMMANDS))
                            {
                                string[] data = response.Marker.Split(new char[] { ':' });
                                if (data[1] == CommonViewModel.Instance.CurrentSet)
                                {
                                    bool isList = false;
                                    bool.TryParse(data[2].ToLower(), out isList);

                                    if (isList)
                                    {
                                        CommonViewModel.Instance.CurrentSetType = "list";
                                    }
                                    else
                                    {
                                        CommonViewModel.Instance.CurrentSetType = "pad";
                                    }

                                    CommandsPageViewModel.Instance.Commands = new System.Collections.ObjectModel.ObservableCollection<string>(JsonConvert.DeserializeObject<List<string>>(response.Content));

                                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                                        {
                                            CommonViewModel.Instance.IsWaiting = false;
                                            App.RootFrame.Navigate(new Uri("/Views/CommandsPage.xaml", UriKind.Relative));
                                        });
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
